
<template>
  <div class="login">
    <div class="login_form">
      <p>用户登录与注册</p>
      <el-tabs v-model="activeName" @tab-click="handleClick" >
        <el-tab-pane label="登录" name="first">
          <el-form
            :model="loginForm"
            :rules="rules"
            ref="loginForm"
            @submit.native.prevent
          >
            <el-form-item label="" prop="account" class="elItem">
              <el-input
                type="text"
                autocomplete="off"
                v-model="loginForm.account"
                prefix-icon="el-icon-user-solid"
                placeholder="请输入用户名"
              ></el-input>
            </el-form-item>
            <el-form-item label="" prop="password">
              <el-input
                type="password"
                autocomplete="off"
                v-model="loginForm.password"
                prefix-icon="el-icon-lock"
                placeholder="请输入密码"
              ></el-input>
            </el-form-item>
            <el-form-item class="btns">
              <el-button type="primary" @click="goToLogin('loginForm')" native-type="submit">登录</el-button>
              <el-button @click="resetLoginForm">重置</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="注册" name="second">
          <register></register>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
  <!--</div>-->
</template>
<script>
  import register from '@/components/register';
  export default {
    data() {
      var validateAccount = (rule, value, callback) => {
        if (value === "") {
          return callback(new Error("账号不能为空"));
        }
      };
      var validatePassword = (rule, value, callback) => {
        if (value === "") {
          callback(new Error("请输入密码"));
        }
      };
      return {
        loginForm: {
          account: "",
          password: ""
        },
        activeName:'first',//默认显示登录页面
        rules: {
          account: [

            { required: true, message: '请您输入用户名', trigger: 'change' },

          ],
          password: [
            { required: true, message: '请您输入密码', trigger: 'change' },
          ],
        },
      };
    },
    methods: {
      goToLogin(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$axios.get("/api/user/denglu/"+this.loginForm.account+"/"+this.loginForm.password).then(res=>{
                  if (res.data.code==101){
                    alert("登录成功!");
                    this.$router.push("/index")
                  }else {
                    alert("登录失败!");
                  }
                })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetLoginForm() {
        this.$refs["loginForm"].resetFields();
      },
      handleClick(){}
    },
    components:{
      register
    }
  };

</script>
